package com.testMain;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author 贾明祯
 * @ClassName TextScan
 * @Date 2020/9/23 13:34
 * @Introduce
 */
public class CalculatorScan {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);

        StringBuilder sbd = new StringBuilder();
        int total = 0;
        int correct = 0;
        while (true){
            String p = "";
            String symbol= "";
            int numOne = 0;
            int numTow = 0;
            int randm = (int)(Math.random()*4);

            Map<String,Integer> map  = new CalculatorScan().nums(randm);
            numOne = map.get("one");
            numTow = map.get("tow");
            int sum =0;
            switch (randm){
                case 0:
                    symbol="+";
                    sum = numOne+numTow;
                    break;
                case 1:
                    symbol="-";
                    sum = numOne-numTow;
                    break;
                case 2:
                    symbol="x";
                    sum = numOne*numTow;
                    break;
                case 3:
                    symbol="/";
                    sum = numOne/numTow;
                    break;
            }
            p= "计算"+numOne+symbol+numTow+"=";
            System.out.println(p);
            System.out.println("请输入");
            String sys = s.next();
            if("esc".equals(sys)){
                break;
            }
            total+=1;
            if(Integer.parseInt(sys) == sum ? true:false){
                System.out.println("输入正确！答案是："+sum);
                correct+=1;
            }else {
                sbd.append(p+sys+",答案："+sum+";");
                System.out.println("输入有误！答案是："+sum);
            }
        }
        System.out.println("共做："+total+"题");
        System.out.println("答对了："+correct+"题");
        System.out.println("答错了："+(total-correct)+"题");
        System.out.println("错误题："+sbd.toString());
    }
    private Map<String, Integer> nums(int i){
        int numOne = 0;
        int numTow = 0;
        Map<String,Integer> map = new HashMap<>();
        switch (i){
            case 1:
                while (true){
                    numOne =  (int)(Math.random()*10);
                    numTow =  (int)(Math.random()*10);
                    if(numOne>numTow){
                        break;
                    }
                }
                break;
            case 3:
                while (true){
                    numOne =  (int)(Math.random()*10);
                    numTow =  (int)(Math.random()*10+1);
                    if(numOne%numTow==0){
                        break;
                    }
                }
                break;
            default:
                numOne =  (int)(Math.random()*10);
                numTow =  (int)(Math.random()*10);
                break;
        }
        map.put("one",numOne);
        map.put("tow",numTow);
        return map;
    }
}
